// Package controllers
// @Title  article.go
// @Description  文章
// @Author  小斌 (时间 格式是2021/12/02  11:40)
// @Update  小斌 (时间 格式是2021/12/02  11:40)

package controllers

import (
	"beego-admin/library"
	"beego-admin/models"
	"strconv"
	"strings"
	"time"
)

type ArticleController struct {
	BaseController
}

func (this *ArticleController) Index() {
	articleCatList,_ := models.GetUseArticleCatList()

	this.Data["article_cat_list"] = articleCatList
	this.TplName = "admin/article/article_list.tpl"
}

func (this *ArticleController) ArticleAdd() {
	articleCatList,_ := models.GetUseArticleCatList()
	articleTagsList,_ := models.GetUseArticleTagsList()
	this.Data["article_cat_list"] = articleCatList
	this.Data["article_tags_list"] = articleTagsList
	this.TplName = "admin/article/article_add.tpl"
}

func (this *ArticleController) ArticleEdit() {
	id,_ := this.GetInt("id",0)

	articleCatList,_ := models.GetUseArticleCatList()
	articleTagsList,_ := models.GetUseArticleTagsList()
	articleCatInfo,_ := models.GetArticleInfoById(id)

	var tagsMap = make(map[int]int)
	if articleCatInfo.Tags != ""{
		tagsArr := strings.Split(articleCatInfo.Tags, ",")
		for _,v := range tagsArr{
			num,_ := strconv.Atoi(v)
			tagsMap[num] = 1
		}
	}

	type tagsList struct {
		Id	int    `json:"id"`
		TagsName	string    `json:"tag_name"`
		Checked	bool    `json:"checked"`
	}
	var tagsListData []tagsList
	for _,v := range *articleTagsList{
		checked := false
		if _, ok :=tagsMap[v.Id]; ok{
			checked = true
		}
		tmpParam := tagsList{v.Id,v.TagsName,checked}
		tagsListData = append(tagsListData, tmpParam)
	}

	this.Data["article_cat_list"] = articleCatList
	this.Data["article_tags_list"] = tagsListData
	this.Data["article_info"] = articleCatInfo
	this.Data["tag_map"] = tagsMap
	this.TplName = "admin/article/article_edit.tpl"
}

func (this *ArticleController) GetArticleList(){
	page,_ := this.GetInt("page",1)
	pageSize,_ := this.GetInt("limit",5)
	articleCat,_ := this.GetInt("article_cat",0)
	queryParams := models.ArticleQueryParams{
		PageSize: pageSize,
		Page:  page,
		CatId:  articleCat,
	}
	list, count, _ := models.GetArticleList(queryParams)

	type ArticleObj struct{
		Id                 int    `json:"id"`
		Title				string `json:"title"`
		Author				string `json:"author"`
		CatId               int `json:"cat_id"`
		CatName               string `json:"cat_name"`
		Tags               string `json:"tags"`
		TagsName               string `json:"tags_name"`
		HomeRecommend               int `json:"home_recommend"`
		HomeRecommendName               string `json:"home_recommend_name"`
		BloggerRecommend               int `json:"blogger_recommend"`
		BloggerRecommendName               string `json:"blogger_recommend_name"`
		Status               int `json:"status"`
		StatusName               string `json:"status_name"`
		UpdateTime                time.Time `json:"update_time"`
		AddTime                time.Time `json:"add_time"`
	}
	var returnData[]ArticleObj
	homeRecommendName := ""
	bloggerRecommendName := "是"
	for _, v := range *list {
		statusName := "启用"
		if v.Status != 0{
			statusName = "停用"
		}

		catName := ""
		if v.CatId > 0{
			articleCatInfo,_ := models.GetArticleCatInfoById(v.CatId)
			catName = articleCatInfo.CatName
		}

		tagsName := ""
		if v.Tags != ""{
			tagsArr := strings.Split(v.Tags, ",")
			articleCatList,_ := models.GetArticleTagsListByIds(library.String2Int(tagsArr))
			for _,v := range *articleCatList{
				tagsName += v.TagsName + ","
			}
			tagsName = tagsName[0:len(tagsName)-1]
		}


		if v.HomeRecommend == 1{
			homeRecommendName = "否"
		}else{
			homeRecommendName = "是"
		}

		if v.BloggerRecommend == 1{
			bloggerRecommendName = "否"
		}else{
			bloggerRecommendName = "是"
		}

		authorName := ""
		if v.Author > 0{
			adminUserInfo,_ := models.GetAdminUserInfoById(v.Author)
			authorName = adminUserInfo.AdminUser
		}

		tmpParam := ArticleObj{v.Id,v.Title,authorName,v.CatId,catName,v.Tags,tagsName,v.HomeRecommend,homeRecommendName,v.BloggerRecommend,bloggerRecommendName,v.Status,statusName,v.UpdateTime,v.AddTime}
		returnData = append(returnData, tmpParam)
	}


	this.Data["json"] = this.AjaxLayuiTableReturn(0,"成功",count,returnData)
	this.ServeJSON()
}

func (this *ArticleController) AjaxAddArticle() {
	title := this.GetString("title")
	introduction := this.GetString("introduction")
	catId,_ := this.GetInt("cat_id",0)
	imageUrl := this.GetString("image_url")
	homeRecommend,_ := this.GetInt("home_recommend",0)
	bloggerRecommend,_ := this.GetInt("blogger_recommend",0)
	contents := this.GetString("contents")
	tags := this.GetString("tags")
	status,_ := this.GetInt("status",0)

	if title == ""{
		this.Data["json"] = this.AjaxReturn(10001,"标题不能为空",nil)
		this.ServeJSON()
		this.StopRun()
	}
	if introduction == ""{
		this.Data["json"] = this.AjaxReturn(10001,"简介不能为空",nil)
		this.ServeJSON()
		this.StopRun()
	}
	if catId == 0{
		this.Data["json"] = this.AjaxReturn(10001,"目录必须选择",nil)
		this.ServeJSON()
		this.StopRun()
	}
	if imageUrl == ""{
		this.Data["json"] = this.AjaxReturn(10001,"图片不能为空",nil)
		this.ServeJSON()
		this.StopRun()
	}
	if contents == ""{
		this.Data["json"] = this.AjaxReturn(10001,"正文不能为空",nil)
		this.ServeJSON()
		this.StopRun()
	}

	author := this.GetSession("user_id")
	id := models.AddArticle(title,author.(int),introduction,catId,tags,imageUrl,homeRecommend,bloggerRecommend,contents,status)
	if id > 0{
		this.Data["json"] = this.AjaxReturn(10000,"添加成功",nil)
	}else{
		this.Data["json"] = this.AjaxReturn(10001,"添加失败",nil)
	}

	this.ServeJSON()
}

func (this *ArticleController) AjaxEditArticle() {
	articleId,_ := this.GetInt("id",0)
	title := this.GetString("title")
	introduction := this.GetString("introduction")
	catId,_ := this.GetInt("cat_id",0)
	imageUrl := this.GetString("image_url")
	homeRecommend,_ := this.GetInt("home_recommend",0)
	bloggerRecommend,_ := this.GetInt("blogger_recommend",0)
	content := this.GetString("content")
	tags := this.GetString("tags")
	status,_ := this.GetInt("status",0)
	if title == ""{
		this.Data["json"] = this.AjaxReturn(10001,"标题不能为空",nil)
		this.ServeJSON()
		this.StopRun()
	}
	if introduction == ""{
		this.Data["json"] = this.AjaxReturn(10001,"简介不能为空",nil)
		this.ServeJSON()
		this.StopRun()
	}
	if catId == 0{
		this.Data["json"] = this.AjaxReturn(10001,"目录必须选择",nil)
		this.ServeJSON()
		this.StopRun()
	}
	if imageUrl == ""{
		this.Data["json"] = this.AjaxReturn(10001,"图片不能为空",nil)
		this.ServeJSON()
		this.StopRun()
	}
	if content == ""{
		this.Data["json"] = this.AjaxReturn(10001,"正文不能为空",nil)
		this.ServeJSON()
		this.StopRun()
	}

	id := models.UpdateArticleById(articleId,title,introduction,catId,tags,imageUrl,homeRecommend,bloggerRecommend,content,status)
	if id > 0{
		this.Data["json"] = this.AjaxReturn(10000,"修改成功",nil)
	}else{
		this.Data["json"] = this.AjaxReturn(10001,"修改失败",nil)
	}

	this.ServeJSON()
}

// AjaxDeleteArticle ajax删除文章
func (this *ArticleController) AjaxDeleteArticle() {
	articleId,_ := this.GetInt("id",0)

	articleInfo,_ := models.GetArticleInfoById(articleId)
	if articleInfo.Id <= 0{
		this.Data["json"] = this.AjaxReturn(10000,"删除的记录不存在",nil)
		this.ServeJSON()
		this.StopRun()
	}
	id := models.DeleteArticleById(articleId)
	if id > 0{
		this.Data["json"] = this.AjaxReturn(10000,"删除成功",nil)
	}else{
		this.Data["json"] = this.AjaxReturn(10001,"删除失败",nil)
	}

	this.ServeJSON()
}